package com.googlecode.dgwt.client.dijit.form.base;

import com.google.gwt.dom.client.ButtonElement;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.HasHTML;
import com.googlecode.dgwt.client.dijit.base.FocusWidget;
import com.googlecode.dgwt.client.dijit.base.state.ActiveState;
import com.googlecode.dgwt.client.dijit.base.state.HoverState;
import com.googlecode.dgwt.client.dijit.base.state.WidgetState;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class ButtonBase extends FocusWidget implements HasHTML {

    
    protected Element iconElement; // TODO iconElement

    
    protected ButtonBase(boolean ready) {
        super(ready);
        requireEventState(ActiveState.get()); 
        requireEventState(HoverState.get());
    }


    public ButtonBase(Element element) {
        this(false);
        setDomElement(element);
        postCreate();
    }

    
    public void setIconClass(String value) {
        iconElement.setClassName(value);
    }

    
    public String getIconClass() {
        return iconElement.getClassName();
    }

    
    @Override
    public String getTemplateParameter(String name) {
        if ("type".equals(name)){
            return "button";
        }
        return super.getTemplateParameter(name);
    }

    
    public ButtonElement getButtonElement() {
        return getAttachedElement("inputElement").cast();
    }


    /**
     * Programmatic equivalent of the user clicking the button.
     */
    public void click() {
        getButtonElement().click();
    }
    
    
    public String getHTML() {
        return getContainerElement().getInnerHTML();
    }

    @Override
    public String getText() {
        return getContainerElement().getInnerText();
    }

    public void setHTML(String html) {
        getContainerElement().setInnerHTML(html);
    }

    @Override
    public void setText(String text) {
        getContainerElement().setInnerText(text);
    }


    @Override
    public void addState(WidgetState state) {
        if (state == HoverState.get()) {
            if (hasState(ActiveState.get())) {
                return;
            }
        }
        else
        if (state == ActiveState.get()) {
            removeState(HoverState.get());
        }
        super.addState(state);
    }

}
